#
# ridge_plots.R
#
# Ridge plots for
# Hill, Seth J., Daniel J. Hopkins, and Gregory A. Huber. 2021. 
# "Not by Turnout Alone: Measuring the Sources of Electoral Change, 2012 to 2016." Science Advances.
#

#### load libraries ----
library(ggplot2)
library(ggridges)
library(gridExtra)
library(readstata13)
library(xtable)
library(haven)
library(dplyr)
library(texreg)

#### load data set ----
dta = data.table(read_dta("PooledPrecinctAnalysisData.dta"))

###
dta$GOPTURNOUTNUM12 <- dta$P12_V12_GOP+dta$PBOTH_V12_GOP+dta$PBOTH_VBOTH_GOP
dta$GOPTURNOUTNUM16 <- dta$P16_V16_GOP+dta$PBOTH_V16_GOP+dta$PBOTH_VBOTH_GOP
dta$GOPTURNOUTNUMD <- dta$GOPTURNOUTNUM16-dta$GOPTURNOUTNUM12

dta$DEMTURNOUTNUM12 <- dta$P12_V12_DEM+dta$PBOTH_V12_DEM+dta$PBOTH_VBOTH_DEM
dta$DEMTURNOUTNUM16 <- dta$P16_V16_DEM+dta$PBOTH_V16_DEM+dta$PBOTH_VBOTH_DEM
dta$DEMTURNOUTNUMD <- dta$DEMTURNOUTNUM16-dta$DEMTURNOUTNUM12

dta$OTHTURNOUTNUM12 <- dta$P12_V12_OTH+dta$PBOTH_V12_OTH+dta$PBOTH_VBOTH_OTH
dta$OTHTURNOUTNUM16 <- dta$P16_V16_OTH+dta$PBOTH_V16_OTH+dta$PBOTH_VBOTH_OTH
dta$OTHTURNOUTNUMD <- dta$OTHTURNOUTNUM16-dta$OTHTURNOUTNUM12

dta$Y12N16  <- dta$PBOTH_V12_DEM+dta$P12_V12_DEM+ 
  dta$PBOTH_V12_OTH+dta$P12_V12_OTH+
  dta$PBOTH_V12_GOP+dta$P12_V12_GOP

dta$N12Y16  <- dta$PBOTH_V16_DEM+dta$P16_V16_DEM+ 
  dta$PBOTH_V16_OTH+dta$P16_V16_OTH+
  dta$PBOTH_V16_GOP+dta$P16_V16_GOP

dta$Y12Y16  <- dta$PBOTH_VBOTH_DEM+ 
  dta$PBOTH_VBOTH_OTH+dta$PBOTH_VBOTH_GOP

#### subset by state
dta.fl <- dta[dta$st=="FL",]
dta.ga <- dta[dta$st=="GA",]
dta.mi <- dta[dta$st=="MI",]
dta.nv <- dta[dta$st=="NV",]
dta.oh <- dta[dta$st=="OH",]
dta.pa <- dta[dta$st=="PA",]

# # #### recode variables
### State prep function
state.prep <- function(dta){
  dta$TURNOUTSHIFTGOP <- (dta$GOPTURNOUTPCT16-dta$GOPTURNOUTPCT12)-(dta$DEMTURNOUTPCT16-dta$DEMTURNOUTPCT12)
  dta$VOTESHIFTGOP <- (dta$GOP_Voters_2016_Prct-dta$GOP_Voters_2012_Prct)-(dta$Dem_Voters_2016_Prct-dta$Dem_Voters_2012_Prct)
  qt <- quantile(dta$TURNOUTSHIFTGOP,seq(from=0,to=1,by=.1))
  dta$TURNOUTSHIFTGOPD <- cut(dta$TURNOUTSHIFTGOP,breaks=qt,labels=seq(from=1,to=10))

  dta$TURNOUTSHIFTNUMGOP <- (dta$GOPTURNOUTNUM16-dta$GOPTURNOUTNUM12)-(dta$DEMTURNOUTNUM16-dta$DEMTURNOUTNUM12)
  qt2 <- quantile(dta$TURNOUTSHIFTNUMGOP,seq(from=0,to=1,by=.1))
  dta$TURNOUTSHIFTGOPNUMD <- cut(dta$TURNOUTSHIFTNUMGOP,breaks=qt2,labels=seq(from=1,to=10))

  dta$SHARETWOELECTIONS <- dta$Y12Y16/(dta$Y12Y16+dta$N12Y16+dta$Y12N16)
  qt3 <- quantile(dta$SHARETWOELECTIONS,seq(from=0,to=1,by=.1))
  dta$SHARETWOELECTIONSD <- cut(dta$SHARETWOELECTIONS,breaks=qt3,labels=seq(from=1,to=10))
 
  dta$PCTVOTESHIFT <- (dta$GOPPCT16-dta$GOPPCT12)-(dta$DEMPCT16-dta$DEMPCT12)
  qt4 <- quantile(dta$PCTVOTESHIFT,seq(from=0,to=1,by=.1),na.rm=T) 
  dta$PCTVOTESHIFTD <- cut(dta$PCTVOTESHIFT,breaks=qt4,labels=seq(from=1,to=10))
    
  return(dta)
}

dta.fl <- state.prep(dta.fl)
qt.fl <- quantile(dta.fl$TURNOUTSHIFTGOP,seq(from=0,to=1,by=.1))
qt.fl.num <- quantile(dta.fl$TURNOUTSHIFTNUMGOP,seq(from=0,to=1,by=.1))
qt.fl.share <- quantile(dta.fl$SHARETWOELECTIONS,seq(from=0,to=1,by=.1))

dta.ga <- state.prep(dta.ga)
qt.ga <- quantile(dta.ga$TURNOUTSHIFTGOP,seq(from=0,to=1,by=.1))
qt.ga.num <- quantile(dta.ga$TURNOUTSHIFTNUMGOP,seq(from=0,to=1,by=.1))
qt.ga.share <- quantile(dta.ga$SHARETWOELECTIONS,seq(from=0,to=1,by=.1))

dta.mi <- state.prep(dta.mi)
qt.mi <- quantile(dta.mi$TURNOUTSHIFTGOP,seq(from=0,to=1,by=.1))
qt.mi.num <- quantile(dta.mi$TURNOUTSHIFTNUMGOP,seq(from=0,to=1,by=.1))
qt.mi.share <- quantile(dta.mi$SHARETWOELECTIONS,seq(from=0,to=1,by=.1))

dta.nv <- state.prep(dta.nv)
qt.nv <- quantile(dta.nv$TURNOUTSHIFTGOP,seq(from=0,to=1,by=.1))
qt.nv.num <- quantile(dta.nv$TURNOUTSHIFTNUMGOP,seq(from=0,to=1,by=.1))
qt.nv.share <- quantile(dta.nv$SHARETWOELECTIONS,seq(from=0,to=1,by=.1))

dta.pa <- state.prep(dta.pa)
qt.pa <- quantile(dta.pa$TURNOUTSHIFTGOP,seq(from=0,to=1,by=.1))
qt.pa.num <- quantile(dta.pa$TURNOUTSHIFTNUMGOP,seq(from=0,to=1,by=.1))
qt.pa.share <- quantile(dta.pa$SHARETWOELECTIONS,seq(from=0,to=1,by=.1))

dta.oh <- state.prep(dta.oh)
qt.oh <- quantile(dta.oh$TURNOUTSHIFTGOP,seq(from=0,to=1,by=.1))
qt.oh.num <- quantile(dta.oh$TURNOUTSHIFTNUMGOP,seq(from=0,to=1,by=.1))
qt.oh.share <- quantile(dta.oh$SHARETWOELECTIONS,seq(from=0,to=1,by=.1))

#### first Figure for turnout share shifts ----
lbs.fl <- paste("From ",round(qt.fl[1:10],digits=2)," to ",round(qt.fl[2:11],digits=2),sep="")
lbs.ga <- paste("From ",round(qt.ga[1:10],digits=2)," to ",round(qt.ga[2:11],digits=2),sep="")
lbs.mi <- paste("From ",round(qt.mi[1:10],digits=2)," to ",round(qt.mi[2:11],digits=2),sep="")
lbs.nv <- paste("From ",round(qt.nv[1:10],digits=2)," to ",round(qt.nv[2:11],digits=2),sep="")
lbs.oh <- paste("From ",round(qt.oh[1:10],digits=2)," to ",round(qt.oh[2:11],digits=2),sep="")

gp.fl <- ggplot(subset(dta.fl,!is.na(TURNOUTSHIFTGOPD)),aes(x=VOTESHIFTGOP,y=TURNOUTSHIFTGOPD))+
  geom_density_ridges(aes(fill = TURNOUTSHIFTGOPD))+ 
  coord_flip()+
  labs(x= "Vote Shift to GOP", y = "Compositional Shift to GOP")+ 
  scale_fill_discrete(name="Pro-GOP Compositional Shift",
                      breaks=c(as.character(1:10)),
                      labels=c(lbs.fl))+
  ggtitle("Florida")+
  xlim(-1000,1000)

density_lines <-
  subset(dta.fl,!is.na(TURNOUTSHIFTGOPD)) %>% 
  group_by(TURNOUTSHIFTGOPD) %>% 
  summarise(x_mean = mean(VOTESHIFTGOP)) %>% 
  mutate(group = as.integer(factor(TURNOUTSHIFTGOPD))) %>% 
  left_join(ggplot_build(gp.fl) %>% purrr::pluck("data", 1), 
            on = "group") %>% 
  group_by(group) %>%
  summarise(x_mean = first(x_mean), 
            density = approx(x, density, first(x_mean))$y, 
            scale = first(scale), 
            iscale = first(iscale))

# add segments and plot
gp.fl +
  geom_segment(aes(x = x_mean,
                   y = group,
                   xend = x_mean,
                   yend = group + density * scale * iscale),
               data = density_lines)+  geom_vline(xintercept=0,linetype="dashed")

gp.ga <- ggplot(subset(dta.ga,!is.na(TURNOUTSHIFTGOPD)),aes(x=VOTESHIFTGOP,y=TURNOUTSHIFTGOPD))+
  geom_density_ridges(aes(fill = TURNOUTSHIFTGOPD))+ 
  coord_flip()+
  labs(x= "Vote Shift to GOP", y = "Compositional Shift to GOP")+ 
  scale_fill_discrete(name="Pro-GOP Compositional Shift",
                      breaks=c(as.character(1:10)),
                      labels=c(lbs.ga))+
  ggtitle("Georgia")+
  xlim(-1000,1000)

gp.mi <- ggplot(subset(dta.mi,!is.na(TURNOUTSHIFTGOPD)),aes(x=VOTESHIFTGOP,y=TURNOUTSHIFTGOPD))+
  geom_density_ridges(aes(fill = TURNOUTSHIFTGOPD))+ 
  coord_flip()+
  labs(x= "Vote Shift to GOP", y = "Compositional Shift to GOP")+ 
  scale_fill_discrete(name="Pro-GOP Compositional Shift",
                      breaks=c(as.character(1:10)),
                      labels=c(lbs.mi))+
  ggtitle("Michigan")#+
  #xlim(-1000,1000)

gp.nv <- ggplot(subset(dta.nv,!is.na(TURNOUTSHIFTGOPD)),aes(x=VOTESHIFTGOP,y=TURNOUTSHIFTGOPD))+
  geom_density_ridges(aes(fill = TURNOUTSHIFTGOPD))+ 
  coord_flip()+
  labs(x= "Vote Shift to GOP", y = "Compositional Shift to GOP")+ 
  scale_fill_discrete(name="Pro-GOP Compositional Shift",
                      breaks=c(as.character(1:10)),
                      labels=c(lbs.nv))+
  ggtitle("Nevada")#+
#xlim(-1000,1000)

gp.oh <- ggplot(subset(dta.oh,!is.na(TURNOUTSHIFTGOPD)),aes(x=VOTESHIFTGOP,y=TURNOUTSHIFTGOPD))+
  geom_density_ridges(aes(fill = TURNOUTSHIFTGOPD))+ 
  coord_flip()+
  labs(x= "Vote Shift to GOP", y = "Compositional Shift to GOP")+ 
  scale_fill_discrete(name="Pro-GOP Compositional Shift",
                      breaks=c(as.character(1:10)),
                      labels=c(lbs.fl))+
  ggtitle("Ohio")

lbs.pa <- paste("From ",round(qt.pa[1:10],digits=2)," to ",round(qt.pa[2:11],digits=2),sep="")

gp.pa <- ggplot(subset(dta.pa,!is.na(TURNOUTSHIFTGOPD)),aes(x=VOTESHIFTGOP,y=TURNOUTSHIFTGOPD))+
  geom_density_ridges(aes(fill = TURNOUTSHIFTGOPD))+ 
  coord_flip()+
  labs(x= "Vote Shift to GOP", y = "Composition Shift to GOP")+ 
  scale_fill_discrete(name="Pro-GOP Composition Shift",
                      breaks=c(as.character(1:10)),
                      labels=c(lbs.pa))+
  ggtitle("Pennsylvania")+
  xlim(-500,500)

pdf("joint-vote-shift-by-turnout-shift-07302020.pdf",width=18)
grid.arrange(gp.fl,gp.ga,gp.mi,gp.nv,gp.oh,gp.pa,nrow=2,ncol=3)
dev.off()

####
#### vote shift x party turnout plot with raw registration numbers ----

lbs.fl <- paste("From ",round(qt.fl.num[1:10],digits=2)," to ",round(qt.fl.num[2:11],digits=2),sep="")
lbs.ga <- paste("From ",round(qt.ga.num[1:10],digits=2)," to ",round(qt.ga.num[2:11],digits=2),sep="")
lbs.mi <- paste("From ",round(qt.mi.num[1:10],digits=2)," to ",round(qt.mi.num[2:11],digits=2),sep="")
lbs.nv <- paste("From ",round(qt.nv.num[1:10],digits=2)," to ",round(qt.nv.num[2:11],digits=2),sep="")
lbs.oh <- paste("From ",round(qt.oh.num[1:10],digits=2)," to ",round(qt.oh.num[2:11],digits=2),sep="")
lbs.pa <- paste("From ",round(qt.pa.num[1:10],digits=2)," to ",round(qt.pa.num[2:11],digits=2),sep="")

###
gp.fl <- ggplot(subset(dta.fl,!is.na(TURNOUTSHIFTGOPNUMD)),aes(x=VOTESHIFTGOP,y=TURNOUTSHIFTGOPNUMD))+
  geom_density_ridges(aes(fill = TURNOUTSHIFTGOPNUMD))+ 
  coord_flip()+
  labs(x= "Vote Shift to GOP", y = "Compositional Shift to GOP")+ 
  scale_fill_discrete(name="Pro-GOP Compositional Shift",
                      breaks=c(as.character(1:10)),
                      labels=c(lbs.fl))+
  ggtitle("Florida")+
  xlim(-1000,1000)

density_lines <-
  subset(dta.fl,!is.na(TURNOUTSHIFTGOPNUMD)) %>% 
  group_by(TURNOUTSHIFTGOPNUMD) %>% 
  summarise(x_mean = mean(VOTESHIFTGOP)) %>% 
  mutate(group = as.integer(factor(TURNOUTSHIFTGOPNUMD))) %>% 
  left_join(ggplot_build(gp.fl) %>% purrr::pluck("data", 1), 
            on = "group") %>% 
  group_by(group) %>%
  summarise(x_mean = first(x_mean), 
            density = approx(x, density, first(x_mean))$y, 
            scale = first(scale), 
            iscale = first(iscale))

# add segments and plot
gp.fl.seg <- gp.fl +
  geom_segment(aes(x = x_mean,
                   y = group,
                   xend = x_mean,
                   yend = group + density * scale * iscale),
               data = density_lines)+  geom_vline(xintercept=0,linetype="dashed")

gp.ga <- ggplot(subset(dta.ga,!is.na(TURNOUTSHIFTGOPD)),aes(x=VOTESHIFTGOP,y=TURNOUTSHIFTGOPD))+
  geom_density_ridges(aes(fill = TURNOUTSHIFTGOPD))+ 
  coord_flip()+
  labs(x= "Vote Shift to GOP", y = "Compositional Shift to GOP")+ 
  scale_fill_discrete(name="Pro-GOP Compositional Shift",
                      breaks=c(as.character(1:10)),
                      labels=c(lbs.ga))+
  ggtitle("Georgia")+
  xlim(-1000,1000)

density_lines <-
  subset(dta.ga,!is.na(TURNOUTSHIFTGOPNUMD) & !is.na(VOTESHIFTGOP)) %>% 
  group_by(TURNOUTSHIFTGOPNUMD) %>% 
  summarise(x_mean = mean(VOTESHIFTGOP)) %>% 
  mutate(group = as.integer(factor(TURNOUTSHIFTGOPNUMD))) %>% 
  left_join(ggplot_build(gp.ga) %>% purrr::pluck("data", 1), 
            on = "group") %>% 
  group_by(group) %>%
  summarise(x_mean = first(x_mean), 
            density = approx(x, density, first(x_mean))$y, 
            scale = first(scale), 
            iscale = first(iscale))

# add segments and plot
gp.ga.seg <- gp.ga +
  geom_segment(aes(x = x_mean,
                   y = group,
                   xend = x_mean,
                   yend = group + density * scale * iscale),
               data = density_lines)+  geom_vline(xintercept=0,linetype="dashed")

gp.mi <- ggplot(subset(dta.mi,!is.na(TURNOUTSHIFTGOPD)),aes(x=VOTESHIFTGOP,y=TURNOUTSHIFTGOPD))+
  geom_density_ridges(aes(fill = TURNOUTSHIFTGOPD))+ 
  coord_flip()+
  labs(x= "Vote Shift to GOP", y = "Compositional Shift to GOP")+ 
  scale_fill_discrete(name="Pro-GOP Compositional Shift",
                      breaks=c(as.character(1:10)),
                      labels=c(lbs.mi))+
  ggtitle("Michigan")#+
#xlim(-1000,1000)

density_lines <-
  subset(dta.mi,!is.na(TURNOUTSHIFTGOPNUMD) & !is.na(VOTESHIFTGOP)) %>% 
  group_by(TURNOUTSHIFTGOPNUMD) %>% 
  summarise(x_mean = mean(VOTESHIFTGOP)) %>% 
  mutate(group = as.integer(factor(TURNOUTSHIFTGOPNUMD))) %>% 
  left_join(ggplot_build(gp.mi) %>% purrr::pluck("data", 1), 
            on = "group") %>% 
  group_by(group) %>%
  summarise(x_mean = first(x_mean), 
            density = approx(x, density, first(x_mean))$y, 
            scale = first(scale), 
            iscale = first(iscale))

# add segments and plot
gp.mi.seg <- gp.mi +
  geom_segment(aes(x = x_mean,
                   y = group,
                   xend = x_mean,
                   yend = group + density * scale * iscale),
               data = density_lines)+  geom_vline(xintercept=0,linetype="dashed")

gp.nv <- ggplot(subset(dta.nv,!is.na(TURNOUTSHIFTGOPD)),aes(x=VOTESHIFTGOP,y=TURNOUTSHIFTGOPD))+
  geom_density_ridges(aes(fill = TURNOUTSHIFTGOPD))+ 
  coord_flip()+
  labs(x= "Vote Shift to GOP", y = "Compositional Shift to GOP")+ 
  scale_fill_discrete(name="Pro-GOP Compositional Shift",
                      breaks=c(as.character(1:10)),
                      labels=c(lbs.nv))+
  ggtitle("Nevada")#+
#xlim(-1000,1000)

density_lines <-
  subset(dta.nv,!is.na(TURNOUTSHIFTGOPNUMD) & !is.na(VOTESHIFTGOP)) %>% 
  group_by(TURNOUTSHIFTGOPNUMD) %>% 
  summarise(x_mean = mean(VOTESHIFTGOP)) %>% 
  mutate(group = as.integer(factor(TURNOUTSHIFTGOPNUMD))) %>% 
  left_join(ggplot_build(gp.nv) %>% purrr::pluck("data", 1), 
            on = "group") %>% 
  group_by(group) %>%
  summarise(x_mean = first(x_mean), 
            density = approx(x, density, first(x_mean))$y, 
            scale = first(scale), 
            iscale = first(iscale))

# add segments and plot
gp.nv.seg <- gp.nv +
  geom_segment(aes(x = x_mean,
                   y = group,
                   xend = x_mean,
                   yend = group + density * scale * iscale),
               data = density_lines)+  geom_vline(xintercept=0,linetype="dashed")


gp.oh <- ggplot(subset(dta.oh,!is.na(TURNOUTSHIFTGOPD)),aes(x=VOTESHIFTGOP,y=TURNOUTSHIFTGOPD))+
  geom_density_ridges(aes(fill = TURNOUTSHIFTGOPD))+ 
  coord_flip()+
  labs(x= "Vote Shift to GOP", y = "Compositional Shift to GOP")+ 
  scale_fill_discrete(name="Pro-GOP Compositional Shift",
                      breaks=c(as.character(1:10)),
                      labels=c(lbs.fl))+
  ggtitle("Ohio")


density_lines <-
  subset(dta.oh,!is.na(TURNOUTSHIFTGOPNUMD) & !is.na(VOTESHIFTGOP)) %>% 
  group_by(TURNOUTSHIFTGOPNUMD) %>% 
  summarise(x_mean = mean(VOTESHIFTGOP)) %>% 
  mutate(group = as.integer(factor(TURNOUTSHIFTGOPNUMD))) %>% 
  left_join(ggplot_build(gp.oh) %>% purrr::pluck("data", 1), 
            on = "group") %>% 
  group_by(group) %>%
  summarise(x_mean = first(x_mean), 
            density = approx(x, density, first(x_mean))$y, 
            scale = first(scale), 
            iscale = first(iscale))

# add segments and plot
gp.oh.seg <- gp.oh +
  geom_segment(aes(x = x_mean,
                   y = group,
                   xend = x_mean,
                   yend = group + density * scale * iscale),
               data = density_lines)+  geom_vline(xintercept=0,linetype="dashed")

gp.pa <- ggplot(subset(dta.pa,!is.na(TURNOUTSHIFTGOPD)),aes(x=VOTESHIFTGOP,y=TURNOUTSHIFTGOPD))+
  geom_density_ridges(aes(fill = TURNOUTSHIFTGOPD))+ 
  coord_flip()+
  labs(x= "Vote Shift to GOP", y = "Compositional Shift to GOP")+ 
  scale_fill_discrete(name="Pro-GOP Compositional Shift",
                      breaks=c(as.character(1:10)),
                      labels=c(lbs.pa))+
  ggtitle("Pennsylvania")+
  xlim(-500,500)

density_lines <-
  subset(dta.pa,!is.na(TURNOUTSHIFTGOPNUMD) & !is.na(VOTESHIFTGOP)) %>% 
  group_by(TURNOUTSHIFTGOPNUMD) %>% 
  summarise(x_mean = mean(VOTESHIFTGOP)) %>% 
  mutate(group = as.integer(factor(TURNOUTSHIFTGOPNUMD))) %>% 
  left_join(ggplot_build(gp.pa) %>% purrr::pluck("data", 1), 
            on = "group") %>% 
  group_by(group) %>%
  summarise(x_mean = first(x_mean), 
            density = approx(x, density, first(x_mean))$y, 
            scale = first(scale), 
            iscale = first(iscale))

# add segments and plot
gp.pa.seg <- gp.pa +
  geom_segment(aes(x = x_mean,
                   y = group,
                   xend = x_mean,
                   yend = group + density * scale * iscale),
               data = density_lines)+  geom_vline(xintercept=0,linetype="dashed")


pdf("joint-vote-shift-by-raw-turnout-shift-07302020.pdf",width=16,height=20)
grid.arrange(gp.fl.seg,gp.ga.seg,gp.mi.seg,gp.nv.seg,gp.oh.seg,gp.pa.seg,nrow=3,ncol=2)
dev.off()



#### Plot with two-election turnout share ----

lbs.fl <- paste("From ",round(qt.fl.share[1:10],digits=2)," to ",round(qt.fl.share[2:11],digits=2),sep="")
lbs.ga <- paste("From ",round(qt.ga.share[1:10],digits=2)," to ",round(qt.ga.share[2:11],digits=2),sep="")
lbs.mi <- paste("From ",round(qt.mi.share[1:10],digits=2)," to ",round(qt.mi.share[2:11],digits=2),sep="")
lbs.nv <- paste("From ",round(qt.nv.share[1:10],digits=2)," to ",round(qt.nv.share[2:11],digits=2),sep="")
lbs.oh <- paste("From ",round(qt.oh.share[1:10],digits=2)," to ",round(qt.oh.share[2:11],digits=2),sep="")
lbs.pa <- paste("From ",round(qt.pa.share[1:10],digits=2)," to ",round(qt.pa.share[2:11],digits=2),sep="")

xtxt <- "Vote Shift to GOP"
ytxt <- "Decile, Compositional Stability"
ltxt <- "Two-Election Voter Share"

gp.fl <- ggplot(subset(dta.fl,!is.na(SHARETWOELECTIONSD)),aes(x=VOTESHIFTGOP,y=SHARETWOELECTIONSD))+
  geom_density_ridges(aes(fill = SHARETWOELECTIONSD))+ 
  coord_flip()+
  labs(x= xtxt, y = ytxt)+ 
  scale_fill_discrete(name=ltxt,
                      breaks=c(as.character(1:10)),
                      labels=c(lbs.fl))+
  ggtitle("Florida")+
  xlim(-1000,1000)

density_lines <-
  subset(dta.fl,!is.na(SHARETWOELECTIONSD)) %>% 
  group_by(SHARETWOELECTIONSD) %>% 
  summarise(x_mean = mean(VOTESHIFTGOP)) %>% 
  mutate(group = as.integer(factor(SHARETWOELECTIONSD))) %>% 
  left_join(ggplot_build(gp.fl) %>% purrr::pluck("data", 1), 
            on = "group") %>% 
  group_by(group) %>%
  summarise(x_mean = first(x_mean), 
            density = approx(x, density, first(x_mean))$y, 
            scale = first(scale), 
            iscale = first(iscale))

# add segments and plot
gp.fl.seg <- gp.fl +
  geom_segment(aes(x = x_mean,
                   y = group,
                   xend = x_mean,
                   yend = group + density * scale * iscale),
               data = density_lines)+  geom_vline(xintercept=0,linetype="dashed")

### ga
gp.ga <- ggplot(subset(dta.ga,!is.na(SHARETWOELECTIONSD)),aes(x=VOTESHIFTGOP,y=SHARETWOELECTIONSD))+
  geom_density_ridges(aes(fill = SHARETWOELECTIONSD))+ 
  coord_flip()+
  labs(x= xtxt, y = ytxt)+ 
  scale_fill_discrete(name=ltxt,
                      breaks=c(as.character(1:10)),
                      labels=c(lbs.ga))+
  ggtitle("Georgia")+
  xlim(-1000,1000)

density_lines <-
  subset(dta.ga,!is.na(SHARETWOELECTIONSD)  & !is.na(VOTESHIFTGOP)) %>% 
  group_by(SHARETWOELECTIONSD) %>% 
  summarise(x_mean = mean(VOTESHIFTGOP)) %>% 
  mutate(group = as.integer(factor(SHARETWOELECTIONSD))) %>% 
  left_join(ggplot_build(gp.ga) %>% purrr::pluck("data", 1), 
            on = "group") %>% 
  group_by(group) %>%
  summarise(x_mean = first(x_mean), 
            density = approx(x, density, first(x_mean))$y, 
            scale = first(scale), 
            iscale = first(iscale))

# add segments and plot
gp.ga.seg <- gp.ga +
  geom_segment(aes(x = x_mean,
                   y = group,
                   xend = x_mean,
                   yend = group + density * scale * iscale),
               data = density_lines)+  geom_vline(xintercept=0,linetype="dashed")


gp.mi <- ggplot(subset(dta.mi,!is.na(SHARETWOELECTIONSD)),aes(x=VOTESHIFTGOP,y=SHARETWOELECTIONSD))+
  geom_density_ridges(aes(fill = SHARETWOELECTIONSD))+ 
  coord_flip()+
  labs(x= xtxt, y = ytxt)+ 
  scale_fill_discrete(name=ltxt,
                      breaks=c(as.character(1:10)),
                      labels=c(lbs.mi))+
  ggtitle("Michigan")#+
  #xlim(-1000,1000)

density_lines <-
  subset(dta.mi,!is.na(SHARETWOELECTIONSD)  & !is.na(VOTESHIFTGOP)) %>% 
  group_by(SHARETWOELECTIONSD) %>% 
  summarise(x_mean = mean(VOTESHIFTGOP)) %>% 
  mutate(group = as.integer(factor(SHARETWOELECTIONSD))) %>% 
  left_join(ggplot_build(gp.mi) %>% purrr::pluck("data", 1), 
            on = "group") %>% 
  group_by(group) %>%
  summarise(x_mean = first(x_mean), 
            density = approx(x, density, first(x_mean))$y, 
            scale = first(scale), 
            iscale = first(iscale))

# add segments and plot
gp.mi.seg <- gp.mi +
  geom_segment(aes(x = x_mean,
                   y = group,
                   xend = x_mean,
                   yend = group + density * scale * iscale),
               data = density_lines)+  geom_vline(xintercept=0,linetype="dashed")


### Nevada 

gp.nv <- ggplot(subset(dta.nv,!is.na(SHARETWOELECTIONSD)),aes(x=VOTESHIFTGOP,y=SHARETWOELECTIONSD))+
  geom_density_ridges(aes(fill = SHARETWOELECTIONSD))+ 
  coord_flip()+
  labs(x= xtxt, y = ytxt)+ 
  scale_fill_discrete(name=ltxt,
                      breaks=c(as.character(1:10)),
                      labels=c(lbs.nv))+
  ggtitle("Nevada")#+
#xlim(-1000,1000)

density_lines <-
  subset(dta.nv,!is.na(SHARETWOELECTIONSD)  & !is.na(VOTESHIFTGOP)) %>% 
  group_by(SHARETWOELECTIONSD) %>% 
  summarise(x_mean = mean(VOTESHIFTGOP)) %>% 
  mutate(group = as.integer(factor(SHARETWOELECTIONSD))) %>% 
  left_join(ggplot_build(gp.nv) %>% purrr::pluck("data", 1), 
            on = "group") %>% 
  group_by(group) %>%
  summarise(x_mean = first(x_mean), 
            density = approx(x, density, first(x_mean))$y, 
            scale = first(scale), 
            iscale = first(iscale))

# add segments and plot
gp.nv.seg <- gp.nv +
  geom_segment(aes(x = x_mean,
                   y = group,
                   xend = x_mean,
                   yend = group + density * scale * iscale),
               data = density_lines)+  geom_vline(xintercept=0,linetype="dashed")


gp.oh <- ggplot(subset(dta.oh,!is.na(SHARETWOELECTIONSD)),aes(x=VOTESHIFTGOP,y=SHARETWOELECTIONSD))+
  geom_density_ridges(aes(fill = SHARETWOELECTIONSD))+ 
  coord_flip()+
  labs(x= xtxt, y = ytxt)+ 
  scale_fill_discrete(name=ltxt,
                      breaks=c(as.character(1:10)),
                      labels=c(lbs.oh))+
  ggtitle("Ohio")#+
  #xlim(-1000,1000)

density_lines <-
  subset(dta.oh,!is.na(SHARETWOELECTIONSD)  & !is.na(VOTESHIFTGOP)) %>% 
  group_by(SHARETWOELECTIONSD) %>% 
  summarise(x_mean = mean(VOTESHIFTGOP)) %>% 
  mutate(group = as.integer(factor(SHARETWOELECTIONSD))) %>% 
  left_join(ggplot_build(gp.oh) %>% purrr::pluck("data", 1), 
            on = "group") %>% 
  group_by(group) %>%
  summarise(x_mean = first(x_mean), 
            density = approx(x, density, first(x_mean))$y, 
            scale = first(scale), 
            iscale = first(iscale))

# add segments and plot
gp.oh.seg <- gp.oh +
  geom_segment(aes(x = x_mean,
                   y = group,
                   xend = x_mean,
                   yend = group + density * scale * iscale),
               data = density_lines)+  geom_vline(xintercept=0,linetype="dashed")

gp.pa <- ggplot(subset(dta.pa,!is.na(SHARETWOELECTIONSD)),aes(x=VOTESHIFTGOP,y=SHARETWOELECTIONSD))+
  geom_density_ridges(aes(fill = SHARETWOELECTIONSD))+ 
  coord_flip()+
  labs(x= xtxt, y = ytxt)+ 
  scale_fill_discrete(name=ltxt,
                      breaks=c(as.character(1:10)),
                      labels=c(lbs.pa))+
  ggtitle("Pennsylvania")+
  xlim(-500,500)

density_lines <-
  subset(dta.pa,!is.na(SHARETWOELECTIONSD)  & !is.na(VOTESHIFTGOP)) %>% 
  group_by(SHARETWOELECTIONSD) %>% 
  summarise(x_mean = mean(VOTESHIFTGOP)) %>% 
  mutate(group = as.integer(factor(SHARETWOELECTIONSD))) %>% 
  left_join(ggplot_build(gp.pa) %>% purrr::pluck("data", 1), 
            on = "group") %>% 
  group_by(group) %>%
  summarise(x_mean = first(x_mean), 
            density = approx(x, density, first(x_mean))$y, 
            scale = first(scale), 
            iscale = first(iscale))

# add segments and plot
gp.pa.seg <- gp.pa +
  geom_segment(aes(x = x_mean,
                   y = group,
                   xend = x_mean,
                   yend = group + density * scale * iscale),
               data = density_lines)+  geom_vline(xintercept=0,linetype="dashed")


pdf("joint-vote-shift-by-2election-turnout-shift-07302020.pdf",width=16,height=20)
grid.arrange(gp.fl.seg,gp.ga.seg,gp.mi.seg,gp.nv.seg,gp.oh.seg,gp.pa.seg,nrow=3,ncol=2)
dev.off()


#### Plot with two-election turnout share; DV=vote percent ----

lbs.fl <- paste("From ",round(qt.fl.share[1:10],digits=2)," to ",round(qt.fl.share[2:11],digits=2),sep="")
lbs.ga <- paste("From ",round(qt.ga.share[1:10],digits=2)," to ",round(qt.ga.share[2:11],digits=2),sep="")
lbs.mi <- paste("From ",round(qt.mi.share[1:10],digits=2)," to ",round(qt.mi.share[2:11],digits=2),sep="")
lbs.nv <- paste("From ",round(qt.nv.share[1:10],digits=2)," to ",round(qt.nv.share[2:11],digits=2),sep="")
lbs.oh <- paste("From ",round(qt.oh.share[1:10],digits=2)," to ",round(qt.oh.share[2:11],digits=2),sep="")
lbs.pa <- paste("From ",round(qt.pa.share[1:10],digits=2)," to ",round(qt.pa.share[2:11],digits=2),sep="")

xtxt <- "Pct. Vote Shift to GOP"
ytxt <- "Decile, Compositional Stability"
ltxt <- "Two-Election Voter Share"

gp.fl <- ggplot(subset(dta.fl,!is.na(SHARETWOELECTIONSD)),aes(x=PCTVOTESHIFT,y=SHARETWOELECTIONSD))+
  geom_density_ridges(aes(fill = SHARETWOELECTIONSD))+ 
  coord_flip()+
  labs(x= xtxt, y = ytxt)+ 
  scale_fill_discrete(name=ltxt,
                      breaks=c(as.character(1:10)),
                      labels=c(lbs.fl))+
  ggtitle("Florida")#+
  #xlim(-1000,1000)

density_lines <-
  subset(dta.fl,!is.na(SHARETWOELECTIONSD)) %>% 
  group_by(SHARETWOELECTIONSD) %>% 
  summarise(x_mean = mean(PCTVOTESHIFT)) %>% 
  mutate(group = as.integer(factor(SHARETWOELECTIONSD))) %>% 
  left_join(ggplot_build(gp.fl) %>% purrr::pluck("data", 1), 
            on = "group") %>% 
  group_by(group) %>%
  summarise(x_mean = first(x_mean), 
            density = approx(x, density, first(x_mean))$y, 
            scale = first(scale), 
            iscale = first(iscale))

# add segments and plot
gp.fl.seg <- gp.fl +
  geom_segment(aes(x = x_mean,
                   y = group,
                   xend = x_mean,
                   yend = group + density * scale * iscale),
               data = density_lines)+  geom_vline(xintercept=0,linetype="dashed")

### ga
gp.ga <- ggplot(subset(dta.ga,!is.na(SHARETWOELECTIONSD)),aes(x=PCTVOTESHIFT,y=SHARETWOELECTIONSD))+
  geom_density_ridges(aes(fill = SHARETWOELECTIONSD))+ 
  coord_flip()+
  labs(x= xtxt, y = ytxt)+ 
  scale_fill_discrete(name=ltxt,
                      breaks=c(as.character(1:10)),
                      labels=c(lbs.ga))+
  ggtitle("Georgia")#+
  #xlim(-1000,1000)

density_lines <-
  subset(dta.ga,!is.na(SHARETWOELECTIONSD)  & !is.na(PCTVOTESHIFT)) %>% 
  group_by(SHARETWOELECTIONSD) %>% 
  summarise(x_mean = mean(PCTVOTESHIFT)) %>% 
  mutate(group = as.integer(factor(SHARETWOELECTIONSD))) %>% 
  left_join(ggplot_build(gp.ga) %>% purrr::pluck("data", 1), 
            on = "group") %>% 
  group_by(group) %>%
  summarise(x_mean = first(x_mean), 
            density = approx(x, density, first(x_mean))$y, 
            scale = first(scale), 
            iscale = first(iscale))

# add segments and plot
gp.ga.seg <- gp.ga +
  geom_segment(aes(x = x_mean,
                   y = group,
                   xend = x_mean,
                   yend = group + density * scale * iscale),
               data = density_lines)+  geom_vline(xintercept=0,linetype="dashed")


gp.mi <- ggplot(subset(dta.mi,!is.na(SHARETWOELECTIONSD)),aes(x=PCTVOTESHIFT,y=SHARETWOELECTIONSD))+
  geom_density_ridges(aes(fill = SHARETWOELECTIONSD))+ 
  coord_flip()+
  labs(x= xtxt, y = ytxt)+ 
  scale_fill_discrete(name=ltxt,
                      breaks=c(as.character(1:10)),
                      labels=c(lbs.mi))+
  ggtitle("Michigan")#+
#xlim(-1000,1000)

density_lines <-
  subset(dta.mi,!is.na(SHARETWOELECTIONSD)  & !is.na(PCTVOTESHIFT)) %>% 
  group_by(SHARETWOELECTIONSD) %>% 
  summarise(x_mean = mean(PCTVOTESHIFT)) %>% 
  mutate(group = as.integer(factor(SHARETWOELECTIONSD))) %>% 
  left_join(ggplot_build(gp.mi) %>% purrr::pluck("data", 1), 
            on = "group") %>% 
  group_by(group) %>%
  summarise(x_mean = first(x_mean), 
            density = approx(x, density, first(x_mean))$y, 
            scale = first(scale), 
            iscale = first(iscale))

# add segments and plot
gp.mi.seg <- gp.mi +
  geom_segment(aes(x = x_mean,
                   y = group,
                   xend = x_mean,
                   yend = group + density * scale * iscale),
               data = density_lines)+  geom_vline(xintercept=0,linetype="dashed")


### Nevada 
gp.nv <- ggplot(subset(dta.nv,!is.na(SHARETWOELECTIONSD)),aes(x=PCTVOTESHIFT,y=SHARETWOELECTIONSD))+
  geom_density_ridges(aes(fill = SHARETWOELECTIONSD))+ 
  coord_flip()+
  labs(x= xtxt, y = ytxt)+ 
  scale_fill_discrete(name=ltxt,
                      breaks=c(as.character(1:10)),
                      labels=c(lbs.nv))+
  ggtitle("Nevada")#+
#xlim(-1000,1000)

density_lines <-
  subset(dta.nv,!is.na(SHARETWOELECTIONSD)  & !is.na(PCTVOTESHIFT)) %>% 
  group_by(SHARETWOELECTIONSD) %>% 
  summarise(x_mean = mean(PCTVOTESHIFT)) %>% 
  mutate(group = as.integer(factor(SHARETWOELECTIONSD))) %>% 
  left_join(ggplot_build(gp.nv) %>% purrr::pluck("data", 1), 
            on = "group") %>% 
  group_by(group) %>%
  summarise(x_mean = first(x_mean), 
            density = approx(x, density, first(x_mean))$y, 
            scale = first(scale), 
            iscale = first(iscale))

# add segments and plot
gp.nv.seg <- gp.nv +
  geom_segment(aes(x = x_mean,
                   y = group,
                   xend = x_mean,
                   yend = group + density * scale * iscale),
               data = density_lines)+  geom_vline(xintercept=0,linetype="dashed")


gp.oh <- ggplot(subset(dta.oh,!is.na(SHARETWOELECTIONSD)),aes(x=PCTVOTESHIFT,y=SHARETWOELECTIONSD))+
  geom_density_ridges(aes(fill = SHARETWOELECTIONSD))+ 
  coord_flip()+
  labs(x= xtxt, y = ytxt)+ 
  scale_fill_discrete(name=ltxt,
                      breaks=c(as.character(1:10)),
                      labels=c(lbs.oh))+
  ggtitle("Ohio")#+
#xlim(-1000,1000)

density_lines <-
  subset(dta.oh,!is.na(SHARETWOELECTIONSD)  & !is.na(PCTVOTESHIFT)) %>% 
  group_by(SHARETWOELECTIONSD) %>% 
  summarise(x_mean = mean(PCTVOTESHIFT)) %>% 
  mutate(group = as.integer(factor(SHARETWOELECTIONSD))) %>% 
  left_join(ggplot_build(gp.oh) %>% purrr::pluck("data", 1), 
            on = "group") %>% 
  group_by(group) %>%
  summarise(x_mean = first(x_mean), 
            density = approx(x, density, first(x_mean))$y, 
            scale = first(scale), 
            iscale = first(iscale))

# add segments and plot
gp.oh.seg <- gp.oh +
  geom_segment(aes(x = x_mean,
                   y = group,
                   xend = x_mean,
                   yend = group + density * scale * iscale),
               data = density_lines)+  geom_vline(xintercept=0,linetype="dashed")

gp.pa <- ggplot(subset(dta.pa,!is.na(SHARETWOELECTIONSD)),aes(x=PCTVOTESHIFT,y=SHARETWOELECTIONSD))+
  geom_density_ridges(aes(fill = SHARETWOELECTIONSD))+ 
  coord_flip()+
  labs(x= xtxt, y = ytxt)+ 
  scale_fill_discrete(name=ltxt,
                      breaks=c(as.character(1:10)),
                      labels=c(lbs.pa))+
  ggtitle("Pennsylvania")#+
  #xlim(-500,500)

density_lines <-
  subset(dta.pa,!is.na(SHARETWOELECTIONSD)  & !is.na(PCTVOTESHIFT)) %>% 
  group_by(SHARETWOELECTIONSD) %>% 
  summarise(x_mean = mean(PCTVOTESHIFT)) %>% 
  mutate(group = as.integer(factor(SHARETWOELECTIONSD))) %>% 
  left_join(ggplot_build(gp.pa) %>% purrr::pluck("data", 1), 
            on = "group") %>% 
  group_by(group) %>%
  summarise(x_mean = first(x_mean), 
            density = approx(x, density, first(x_mean))$y, 
            scale = first(scale), 
            iscale = first(iscale))

# add segments and plot
gp.pa.seg <- gp.pa +
  geom_segment(aes(x = x_mean,
                   y = group,
                   xend = x_mean,
                   yend = group + density * scale * iscale),
               data = density_lines)+  geom_vline(xintercept=0,linetype="dashed")


pdf("joint-pct-vote-shift-by-2election-turnout-shift-07302020.pdf",width=16,height=20)
grid.arrange(gp.fl.seg,gp.ga.seg,gp.mi.seg,gp.nv.seg,gp.oh.seg,gp.pa.seg,nrow=3,ncol=2)
dev.off()

##### plot GOP turnout share; outcome = vote share ----

lbs.fl <- paste("From ",round(qt.fl[1:10],digits=2)," to ",round(qt.fl[2:11],digits=2),sep="")
lbs.ga <- paste("From ",round(qt.ga[1:10],digits=2)," to ",round(qt.ga[2:11],digits=2),sep="")
lbs.mi <- paste("From ",round(qt.mi[1:10],digits=2)," to ",round(qt.mi[2:11],digits=2),sep="")
lbs.nv <- paste("From ",round(qt.nv[1:10],digits=2)," to ",round(qt.nv[2:11],digits=2),sep="")
lbs.oh <- paste("From ",round(qt.oh[1:10],digits=2)," to ",round(qt.oh[2:11],digits=2),sep="")
lbs.pa <- paste("From ",round(qt.pa[1:10],digits=2)," to ",round(qt.pa[2:11],digits=2),sep="")

xlb <- "Pct. Vote Shift to GOP"
ylb <- "Compositional Shift to GOP"
gp.fl <- ggplot(subset(dta.fl,!is.na(TURNOUTSHIFTGOPD)),aes(x=PCTVOTESHIFT,y=TURNOUTSHIFTGOPD))+
  geom_density_ridges(aes(fill = TURNOUTSHIFTGOPD))+ 
  coord_flip()+
  labs(x= xlb, y = ylb)+ 
  scale_fill_discrete(name="Share Pro-GOP Compositional Shift",
                      breaks=c(as.character(1:10)),
                      labels=c(lbs.fl))+
  ggtitle("Florida")

density_lines <-
  subset(dta.fl,!is.na(TURNOUTSHIFTGOPD)) %>% 
  group_by(TURNOUTSHIFTGOPD) %>% 
  summarise(x_mean = mean(PCTVOTESHIFT)) %>% 
  mutate(group = as.integer(factor(TURNOUTSHIFTGOPD))) %>% 
  left_join(ggplot_build(gp.fl) %>% purrr::pluck("data", 1), 
            on = "group") %>% 
  group_by(group) %>%
  summarise(x_mean = first(x_mean), 
            density = approx(x, density, first(x_mean))$y, 
            scale = first(scale), 
            iscale = first(iscale))

# add segments and plot
gp.fl.seg <- gp.fl +
  geom_segment(aes(x = x_mean,
                   y = group,
                   xend = x_mean,
                   yend = group + density * scale * iscale),
               data = density_lines)+  geom_vline(xintercept=0,linetype="dashed")

gp.ga <- ggplot(subset(dta.ga,!is.na(TURNOUTSHIFTGOPD)),aes(x=PCTVOTESHIFT,y=TURNOUTSHIFTGOPD))+
  geom_density_ridges(aes(fill = TURNOUTSHIFTGOPD))+ 
  coord_flip()+
  labs(x= xlb, y = ylb)+ 
  scale_fill_discrete(name="Pro-GOP Compositional Shift",
                      breaks=c(as.character(1:10)),
                      labels=c(lbs.ga))+
  ggtitle("Georgia")

density_lines <-
  subset(dta.ga,!is.na(TURNOUTSHIFTGOPD) & !is.na(PCTVOTESHIFT)) %>% 
  group_by(TURNOUTSHIFTGOPD) %>% 
  summarise(x_mean = mean(PCTVOTESHIFT)) %>% 
  mutate(group = as.integer(factor(TURNOUTSHIFTGOPD))) %>% 
  left_join(ggplot_build(gp.ga) %>% purrr::pluck("data", 1), 
            on = "group") %>% 
  group_by(group) %>%
  summarise(x_mean = first(x_mean), 
            density = approx(x, density, first(x_mean))$y, 
            scale = first(scale), 
            iscale = first(iscale))

# add segments and plot
gp.ga.seg <- gp.ga +
  geom_segment(aes(x = x_mean,
                   y = group,
                   xend = x_mean,
                   yend = group + density * scale * iscale),
               data = density_lines)+  geom_vline(xintercept=0,linetype="dashed")

gp.mi <- ggplot(subset(dta.mi,!is.na(TURNOUTSHIFTGOPD)),aes(x=PCTVOTESHIFT,y=TURNOUTSHIFTGOPD))+
  geom_density_ridges(aes(fill = TURNOUTSHIFTGOPD))+ 
  coord_flip()+
  labs(x= xlb, y = ylb)+ 
  scale_fill_discrete(name="Pro-GOP Compositional Shift",
                      breaks=c(as.character(1:10)),
                      labels=c(lbs.mi))+
  ggtitle("Michigan")#+
#xlim(-1000,1000)

density_lines <-
  subset(dta.mi,!is.na(TURNOUTSHIFTGOPD) & !is.na(PCTVOTESHIFT)) %>% 
  group_by(TURNOUTSHIFTGOPD) %>% 
  summarise(x_mean = mean(PCTVOTESHIFT)) %>% 
  mutate(group = as.integer(factor(TURNOUTSHIFTGOPD))) %>% 
  left_join(ggplot_build(gp.mi) %>% purrr::pluck("data", 1), 
            on = "group") %>% 
  group_by(group) %>%
  summarise(x_mean = first(x_mean), 
            density = approx(x, density, first(x_mean))$y, 
            scale = first(scale), 
            iscale = first(iscale))

# add segments and plot
gp.mi.seg <- gp.mi +
  geom_segment(aes(x = x_mean,
                   y = group,
                   xend = x_mean,
                   yend = group + density * scale * iscale),
               data = density_lines)+  geom_vline(xintercept=0,linetype="dashed")

gp.nv <- ggplot(subset(dta.nv,!is.na(TURNOUTSHIFTGOPD)),aes(x=PCTVOTESHIFT,y=TURNOUTSHIFTGOPD))+
  geom_density_ridges(aes(fill = TURNOUTSHIFTGOPD))+ 
  coord_flip()+
  labs(x= xlb, y = ylb)+ 
  scale_fill_discrete(name="Pro-GOP Compositional Shift",
                      breaks=c(as.character(1:10)),
                      labels=c(lbs.nv))+
  ggtitle("Nevada")#+
#xlim(-1000,1000)

density_lines <-
  subset(dta.nv,!is.na(TURNOUTSHIFTGOPD) & !is.na(PCTVOTESHIFT)) %>% 
  group_by(TURNOUTSHIFTGOPD) %>% 
  summarise(x_mean = mean(PCTVOTESHIFT)) %>% 
  mutate(group = as.integer(factor(TURNOUTSHIFTGOPD))) %>% 
  left_join(ggplot_build(gp.nv) %>% purrr::pluck("data", 1), 
            on = "group") %>% 
  group_by(group) %>%
  summarise(x_mean = first(x_mean), 
            density = approx(x, density, first(x_mean))$y, 
            scale = first(scale), 
            iscale = first(iscale))

# add segments and plot
gp.nv.seg <- gp.nv +
  geom_segment(aes(x = x_mean,
                   y = group,
                   xend = x_mean,
                   yend = group + density * scale * iscale),
               data = density_lines)+  geom_vline(xintercept=0,linetype="dashed")

gp.oh <- ggplot(subset(dta.oh,!is.na(TURNOUTSHIFTGOPD)),aes(x=PCTVOTESHIFT,y=TURNOUTSHIFTGOPD))+
  geom_density_ridges(aes(fill = TURNOUTSHIFTGOPD))+ 
  coord_flip()+
  labs(x= xlb, y = "Compositional Shift to GOP")+ 
  scale_fill_discrete(name="Pro-GOP Compositional Shift",
                      breaks=c(as.character(1:10)),
                      labels=c(lbs.fl))+
  ggtitle("Ohio")


density_lines <-
  subset(dta.oh,!is.na(TURNOUTSHIFTGOPD) & !is.na(PCTVOTESHIFT)) %>% 
  group_by(TURNOUTSHIFTGOPD) %>% 
  summarise(x_mean = mean(PCTVOTESHIFT)) %>% 
  mutate(group = as.integer(factor(TURNOUTSHIFTGOPD))) %>% 
  left_join(ggplot_build(gp.oh) %>% purrr::pluck("data", 1), 
            on = "group") %>% 
  group_by(group) %>%
  summarise(x_mean = first(x_mean), 
            density = approx(x, density, first(x_mean))$y, 
            scale = first(scale), 
            iscale = first(iscale))

# add segments and plot
gp.oh.seg <- gp.oh +
  geom_segment(aes(x = x_mean,
                   y = group,
                   xend = x_mean,
                   yend = group + density * scale * iscale),
               data = density_lines)+  geom_vline(xintercept=0,linetype="dashed")

gp.pa <- ggplot(subset(dta.pa,!is.na(PCTVOTESHIFT) & !is.na(TURNOUTSHIFTGOPD)),aes(x=PCTVOTESHIFT,y=TURNOUTSHIFTGOPD))+
  geom_density_ridges(aes(fill = TURNOUTSHIFTGOPD))+ 
  coord_flip()+
  labs(x= xlb, y = ylb)+ 
  scale_fill_discrete(name="Pro-GOP Compositional Shift",
                      breaks=c(as.character(1:10)),
                      labels=c(lbs.pa))+
  ggtitle("Pennsylvania")

density_lines <-
  subset(dta.pa,!is.na(TURNOUTSHIFTGOPD) & !is.na(PCTVOTESHIFT)) %>% 
  group_by(TURNOUTSHIFTGOPD) %>% 
  summarise(x_mean = mean(PCTVOTESHIFT)) %>% 
  mutate(group = as.integer(factor(TURNOUTSHIFTGOPD))) %>% 
  left_join(ggplot_build(gp.pa) %>% purrr::pluck("data", 1), 
            on = "group") %>% 
  group_by(group) %>%
  summarise(x_mean = first(x_mean), 
            density = approx(x, density, first(x_mean))$y, 
            scale = first(scale), 
            iscale = first(iscale))

# add segments and plot
gp.pa.seg <- gp.pa +
  geom_segment(aes(x = x_mean,
                   y = group,
                   xend = x_mean,
                   yend = group + density * scale * iscale),
               data = density_lines)+  geom_vline(xintercept=0,linetype="dashed")


pdf("joint-pct-vote-shift-by-gop-turnout-shift-0730220.pdf",width=16,height=20)
grid.arrange(gp.fl.seg,gp.ga.seg,gp.mi.seg,gp.nv.seg,gp.oh.seg,gp.pa.seg,nrow=3,ncol=2)
dev.off()



